﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Project05
{
    class Object
    {
        private Texture2D texture;
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }

        private Vector2 position;
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        private Vector2 origin;
        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }

        private Vector3 velocity;
        public Vector3 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public float radiusValue { get; set; }
        public Vector3 Radius { get; set; }

        //Animation
        public int Rows { get; set; }
        public int Columns { get; set; }

        private int currentFrame;
        private int totalFrames;        
        private int frameTime;
        private int elapsedTime;
        private bool isLoop;
        private bool looping;

        private Vector2 offset;

        public Object(Texture2D texture, Vector2 position)
        {
            this.texture = texture;
            this.position = position;

            origin = new Vector2(texture.Width / 2, texture.Height / 2);
            offset = new Vector2(450, 450);
        }

        public Object(Vector2 position)
        {
            this.position = position;
        }

        public Object(Texture2D texture, Vector2 position, int rows, int columns,
            int frameTime, bool isLoop)
        {
            this.texture = texture;
            this.position = position;

            Rows = rows;
            Columns = columns;
            currentFrame = 0;
            totalFrames = Rows * Columns;

            this.frameTime = frameTime;            
            this.isLoop = isLoop;

            elapsedTime = 0;
            looping = true;
        }

        public void Update(GameTime gameTime)
        {
            if (looping == false)
                return;

            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (elapsedTime > frameTime)
            {
                currentFrame++;
                if (currentFrame == totalFrames)
                {
                    if (isLoop == false)
                    {
                        looping = false;
                        currentFrame = totalFrames;
                    }
                    else
                        currentFrame = 0;
                }
                elapsedTime = 0;
            }
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 rootCoordinate)
        {
            //Calculate the width and height of the current frame
            int width = Texture.Width / Columns;
            int height = Texture.Height / Rows;

            //Calculate which row and column the current frame is located at
            int row = currentFrame / Columns;
            int column = currentFrame % Columns;

            //Rectangle within the texture that we want to draw
            Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);

            //Rectangle that represents where the texture will be drawn
            Rectangle destinationRectangle = new Rectangle((int)position.X + (int)rootCoordinate.X - width / 2,
                (int)position.Y + (int)rootCoordinate.Y - height / 2, width, height);

            spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);  
        }
    }
}
